package com.caac.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.caac.core.config.Config;
import com.caac.core.config.OrderClass;
import com.caac.core.config.OrderClazz;
import com.caac.core.dao.OrderPropDao;
import com.caac.core.dm.OrderProp;
import com.caac.core.service.OrderPropManager;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2004</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public class OrderPropManagerImpl extends BaseManagerImpl implements
        OrderPropManager {
    private OrderPropDao orderPropDao;

    public void setOrderPropDao(OrderPropDao orderPropDao) {
        this.orderPropDao = orderPropDao;
    }

    public List getUnselectedOrderPropsByGroupName(String groupName) {
        List all = getOrder(groupName).getChildren();
        List selected = getSelectedOrderPropsByGroupName(groupName);
        List unselected = new ArrayList();
        for (int i = 0; i < all.size(); i++) {
            OrderProp op = (OrderProp) all.get(i);
            if (!selected.contains(op)) {
                unselected.add(op);
            }
        }
        return unselected;
    }

    public List getSelectedOrderPropsByGroupName(String groupName) {
        //List props = orderPropDao.getOrderPropsByGroupName(groupName);
        //if (props == null || props.size() == 0) {
            return getOrder(groupName).getDefDmChildren();
        //}
        //return props;
    }

    public OrderClazz getSelectedOrderClazzByGroupName(String groupName) {
        return new OrderClazz(getSelectedOrderPropsByGroupName(groupName));
    }

    public void saveOrderProps(String groupName, String[] propNames) {
        orderPropDao.deleteOrderPropsByGroupName(groupName);
        if (propNames != null) {
            for (int i = 0; i < propNames.length; i++) {
                String propName = propNames[i];
                if (propName != null) {
                    int index = propName.lastIndexOf(".");
                    if (index != -1) {
                        String prefix = propName.substring(0, index);
                        String suffix = propName.substring(index + 1);
                        OrderProp p = new OrderProp();
                        p.setGroupName(groupName);
                        p.setPropName(prefix);
                        p.setPriority(new Integer(i));
                        if ("1".equals(suffix)) {
                            p.setAsc(Boolean.TRUE);
                        } else {
                            p.setAsc(Boolean.FALSE);
                        }
                        orderPropDao.saveOrderProp(p);
                    }
                }
            }
        }
    }

    public List getUnselectedOrderPropsByGroupName(String groupName,
            String username) {
        List all = getOrder(groupName).getChildren();
        List selected = getSelectedOrderPropsByGroupName(groupName, username);
        List unselected = new ArrayList();
        for (int i = 0; i < all.size(); i++) {
            OrderProp op = (OrderProp) all.get(i);
            if (!selected.contains(op)) {
                unselected.add(op);
            }
        }
        return unselected;
    }

    public List getSelectedOrderPropsByGroupName(String groupName,
                                                 String username) {
        //List props = orderPropDao.getOrderPropsByGroupName(groupName, username);
        //if (props == null || props.size() == 0) {
            return getOrder(groupName).getDefDmChildren();
        //}
        //return props;
    }

    public OrderClazz getSelectedOrderClazzByGroupName(String groupName,
            String username) {
        return new OrderClazz(getSelectedOrderPropsByGroupName(groupName,
                username));
    }

    public void saveOrderProps(String groupName, String[] propNames,
                               String username) {
        orderPropDao.deleteOrderPropsByGroupName(groupName, username);
        if (propNames != null) {
            for (int i = 0; i < propNames.length; i++) {
                String propName = propNames[i];
                if (propName != null) {
                    int index = propName.lastIndexOf(".");
                    if (index != -1) {
                        String prefix = propName.substring(0, index);
                        String suffix = propName.substring(index + 1);
                        OrderProp p = new OrderProp();
                        p.setGroupName(groupName);
                        p.setPropName(prefix);
                        p.setPriority(new Integer(i));
                        if ("1".equals(suffix)) {
                            p.setAsc(Boolean.TRUE);
                        } else {
                            p.setAsc(Boolean.FALSE);
                        }
                        p.setUsername(username);
                        orderPropDao.saveOrderProp(p);
                    }
                }
            }
        }
    }

    private static OrderClass getOrder(String groupName) {
        return Config.getInstance().getOrder(groupName);
    }
}
